package com.opensky.qatest.ui.page;

import java.util.List;

import junit.framework.Assert;

import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.InvitePageForm;
import com.opensky.qatest.util.common.AssertReport;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

public class InvitePage extends LoadableComponent<InvitePage> implements IPage {

    // ************************ Locators ************************

    private static final String INVITE_PAGE_URL = "https://opensky.com/welcome/invite";

    private static final String EARN_CREDIT_STEP = "//div[@class='steps']//li[@class='active']/span[text()='Earn Credit']";

    private static final String START_SHOPPING_BTN = "//a[@class='button' and text()='Start shopping']";

    private static final String INVALID_EMAIL_ALERT = "//div[@id='error-message' and contains(., 'There was an error submitting the email invitation form.')]";

    private static final String INVALID_EMAIL_LABEL = "//label[@class='errors' and contains(., 'is not a valid email address')]";

    private static final String SUCCESS_SEND_MESSAGE = "//div[@id='notification']//li[text()='Invitations were sent to: %s']";

    // private static final String ? = "";
    // private static final String ? = "";
    // private static final String ? = "";
    // private static final String ? = "";

    // ************************ Complex page objects **************************

    private InvitePageForm inviteForm;

    private final WebDriverWrapper driver;

    public InvitePage(WebDriverWrapper driver) {
	this.driver = driver;
	inviteForm = new InvitePageForm(driver);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(INVITE_PAGE_URL);
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(AssertReport.wrongURL(INVITE_PAGE_URL, url), LogicUtils.isURLEqual(INVITE_PAGE_URL, url));
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(INVITE_PAGE_URL);
    }

    public boolean isBaseElementsPresent() {
	driver.waitAndCheckElementPresent("EARN_CREDIT_STEP", By.xpath(EARN_CREDIT_STEP));
	driver.waitAndCheckElementPresent("START_SHOPPING_BTN", By.xpath(START_SHOPPING_BTN));
	return true;
    }

    public boolean isInvalidEmailAlertPresent() {
	driver.waitAndCheckElementPresent("INVALID_EMAIL_ALERT", By.xpath(INVALID_EMAIL_ALERT));
	driver.waitAndCheckElementPresent("INVALID_EMAIL_LABEL", By.xpath(INVALID_EMAIL_LABEL));
	return true;
    }

    public boolean isSuccessSendMessagePresent(List<String> toEmails) {

	String emailsText = "";
	for (String toEmail : toEmails) {
	    emailsText += toEmail + ", ";
	}
	emailsText = StringUtils.removeEnd(emailsText, ", ");

	String message = String.format(SUCCESS_SEND_MESSAGE, emailsText);

	driver.waitAndCheckElementPresent("SUCCESS_SEND_MESSAGE", By.xpath(message));

	return true;
    }

    // ************************ GET/SET methods **************************

    public InvitePageForm getInviteForm() {

	if (inviteForm.isInviteFormPresent()) {
	    return inviteForm;
	} else {
	    return null;
	}
    }

    public SalesPage clickStartShoppingBtn() {

	SalesPage salesPage = new SalesPage(driver);

	driver.waitAndClick("START_SHOPPING_BTN", By.xpath(START_SHOPPING_BTN));

	if (salesPage.isPageLoaded()) {
	    return salesPage;
	} else {
	    return null;
	}
    }

}
